package com.example.im.db.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.example.im.bean.ChatRecord;

import java.util.List;

@Dao
public interface ChatRecordDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    Long insert(ChatRecord chatRecord);

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insert(List<ChatRecord> chatRecords);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void update(ChatRecord chatRecord);

    @Delete
    void delete(ChatRecord chatRecord);

    @Query("DELETE FROM ChatRecord WHERE u_id = :uid")
    void delete(int uid);

    @Query("DELETE FROM ChatRecord")
    void delete();

    @Query("SELECT * FROM ChatRecord")
    List<ChatRecord> loadAll();

    @Query("SELECT * FROM ChatRecord WHERE id =:id")
    ChatRecord selectChatRecordByID(int id);

    @Query("SELECT * FROM ChatRecord WHERE encrypt =:encrypt")
    ChatRecord selectChatRecordByEncrypt(String encrypt);

    @Query("SELECT * FROM ChatRecord WHERE u_id =:uid")
    List<ChatRecord> selectChatRecordByUid(int uid);

    @Query("SELECT COUNT(*) FROM ChatRecord WHERE status= 1 AND u_id=:uid")
    int countUnread(int uid);

    @Query("SELECT COUNT(*) FROM ChatRecord WHERE status= 1")
    int countUnread();

    @Query("SELECT time FROM ChatRecord WHERE u_id =:uid AND type = -1 ORDER BY id DESC LIMIT 1")
    long selectChatRecordByUidToLimitTime(int uid);

    @Query("UPDATE ChatRecord SET status= :newStatus WHERE status = :oldStatus AND u_id = :uid")
    void upDateChatRecordStatus(int newStatus, int oldStatus, int uid);

    @Query("UPDATE ChatRecord SET status= 3 WHERE status = 2")
    void upDateChatRecordStatusFail();
}
